package com.xctech.yace.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.java.tuple.Tuple3;

import java.util.UUID;
@Slf4j
public class JsonNodeUtil {
    public JsonNodeUtil() {
    }

    public static ObjectMapper getInstance() {
        return JsonNodeUtil.SingletonClassInstance.INSTANCE;
    }

    private static class SingletonClassInstance {
        private static final ObjectMapper INSTANCE;

        private SingletonClassInstance() {
        }

        static {
            INSTANCE = (new ObjectMapper()).registerModule(new JavaTimeModule()).registerModule(new Jdk8Module()).registerModule(new ParameterNamesModule()).setSerializationInclusion(JsonInclude.Include.NON_NULL);
        }
    }
    /**
     * 从对应路径下找到对应的值（字符串）返回,找不到返回 null
     * @param tuple f0:路径（root开头） f1:字段名  f2:是否可以为空
     * @param root 根JsonNode节点
     * @return
     */
    public static String getValueByPath(Tuple3<String,String,Boolean> tuple, JsonNode root){
        String res = null;
        JsonNode tmp;

        String f0 = tuple.f0;
        String f1 = tuple.f1;
        Boolean f2 = tuple.f2;
        String[] split = f0.split("/");

        if(split.length == 1 && split[0].equals("root")){
            tmp = root;
        }else{
            tmp = root;
            for(int i = 1;i < split.length ;i++){
                tmp = tmp.get(split[i]);
            }
        }

        if(tmp!=null && tmp.has(f1)){
            if(!tmp.get(f1).isNull() && tmp.get(f1).isTextual()) {
                res = tmp.get(f1).textValue().trim();
            }else if(!tmp.get(f1).isNull() && tmp.get(f1).isInt()) {
                //数字类型兼容 场景1：i2的op是数字类型 dsj是字符串
                res = String.valueOf(tmp.get(f1).asInt());
            }else if(!tmp.get(f1).isNull() && tmp.get(f1).isLong()) {
                res = String.valueOf(tmp.get(f1).asLong());
            }else if(!tmp.get(f1).isNull() && tmp.get(f1).isDouble()){
                res = String.valueOf(tmp.get(f1).asDouble());
            }else if(!tmp.get(f1).isNull() && tmp.get(f1).isBoolean()){
                res = String.valueOf(tmp.get(f1).asBoolean());
            }else if(!tmp.get(f1).isNull() && tmp.get(f1).isArray()){
                res = String.valueOf("\""+f1+"\":"+tmp.get(f1).toString());
            }else if(!tmp.get(f1).isNull() && tmp.get(f1).isObject()){
                res = String.valueOf("\""+f1+"\":"+tmp.get(f1).toString());
            }
        }else{
            //log.error("JsonNode找不到对应的值,请检查路径{}和字段{}是否正确！",f0,f1);
        }

        if(f2 && res == null && tmp != null){
            //如果f2允许为空,并且res返回值null就随机生成一个值
            res = UUID.randomUUID().toString().trim();
            //将生成的uuid重新写回JsonNode
            ObjectNode tmpObject = (ObjectNode) tmp;
            tmpObject.put(f1,res);
            log.info("{}随机生成唯一编号{}",f1,res);
        }
        return res;
    }

}
